﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using blackhouse.BlackApart.Business.Apartments;
using System.Collections.ObjectModel;
using blackhouse.BlackApart.Business.Reservations;

namespace blackhouse.BlackApart.Business.Calendar {
    public class ImpApartmentCalendar : ICalendar {

        #region Fields

        private List<IWebReservation> reservations;

        #endregion

        #region Properties

        public System.Collections.ObjectModel.ReadOnlyCollection<IWebReservation> Reservations {
            get {
                if (this.reservations == null) return null;
                return this.reservations.Where(r => r.ReservationId > 0).ToList().AsReadOnly(); 
            }
        }

        public IApartment Apartment { get; private set; }

        public DateTime FromDay { get; private set; }

        public DateTime ToDay { get; private set; }

        #endregion

        #region Constructors

        internal ImpApartmentCalendar(IApartment forApartment, DateTime startDate, DateTime endDate) {
            this.Apartment = forApartment;
            this.FromDay = startDate;
            this.ToDay = endDate;
        }

        #endregion

        #region Methods

        internal void AddReservation(IWebReservation reservation) {
            reservation.Apartment = this.Apartment;
            this.reservations.Add(reservation);
        }

        public IWebReservation CreateReservation() {
            throw new NotImplementedException();
        }

        public int GetDayState(DateTime day) {
            day = new DateTime(day.Year, day.Month, day.Day, 14, 0, 0);
            IWebReservation reservation = this.GetReservationFromDay(day);
            if (reservation != null)
                return 0;
            else
                return this.Apartment.NumberOfPeople;
        }

        private IWebReservation GetReservationFromDay(DateTime day) {
            return this.Reservations.FirstOrDefault(r => r.ArrivalDate <= day && r.DepartureDate >= day);
        }

        public void SetReservations(IWebReservation[] reservations) {
            this.reservations = new List<IWebReservation>(reservations);
        }

        public static DateTime GetStartDate(int month, int year) {
            DateTime startDate = new DateTime(year, month, 1, 14, 0, 0);
            if (startDate < DateTime.Now)
                startDate = DateTime.Today.AddHours(14);
            return startDate;
        }

        public static DateTime GetEndDate(DateTime startDate) {
            DateTime endDate = startDate.AddMonths(1).AddDays(-1).AddHours(-2);
            return endDate;
        }

        #endregion

    }
}
